var map;
var marker;
var infoWnd;

function initialize() {
	var myOptions = {
		zoom : 4,
		center : new google.maps.LatLng(34.2, 108.8),
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
	};
	map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
	
	google.maps.event.addListener(map, 'click', getPos);
}

$(function(){
	initialize();
});


function getPos(evt){
	if(marker){
		marker.setPosition(evt.latLng);
		
		marker.infoWnd.latlng_ = evt.latLng;
		marker.infoWnd.draw();
		
		$('#latlng').text(
				'纬度：'+
				Math.round(evt.latLng.lat()*100000) / 100000
				+', 经度：'
				+Math.round(evt.latLng.lng()*100000) / 100000);
	}else{
		marker = new google.maps.Marker({
	        position: evt.latLng,
	        icon: './images/point.png',
	        draggable: true,
	        map: map
	    });
		
		$('#latlng').text('纬度：'
				+Math.round(evt.latLng.lat()*100000) / 100000
				+', 经度：'
				+Math.round(evt.latLng.lng()*100000) / 100000);
		
		
		var content = '<div class="infobox_content">'
			 +'<img class="thumbnail" src="./images/thumb.jpg"/>'
			 +'<span class="photonum">1张图片</span>'
			 +'<a class="viewlink" href="javascript:void(0)"></a>'
		 	 +'</div>';
		
		var infoBox = new InfoBox({latlng:evt.latLng, map: map, content: content});
		marker.infoWnd = infoBox;
		
		google.maps.event.addListener(marker, 'drag', function(){
			this.infoWnd.latlng_ = this.getPosition();
			this.infoWnd.draw();
			
			$('#latlng').text(
					'纬度：'+
					Math.round(this.getPosition().lat()*100000) / 100000
					+', 经度：'
					+Math.round(this.getPosition().lng()*100000) / 100000);
		});
	}
}